{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### [658\\. Find K Closest Elements](https://leetcode.com/problems/find-k-closest-elements/)\n",
    "\n",
    "Difficulty: **Medium**  \n",
    "\n",
    "Related Topics: [Binary Search](https://leetcode.com/tag/binary-search/)\n",
    "\n",
    "\n",
    "Given a **sorted** integer array `arr`, two integers `k` and `x`, return the `k` closest integers to `x` in the array. The result should also be sorted in ascending order.\n",
    "\n",
    "An integer `a` is closer to `x` than an integer `b` if:\n",
    "\n",
    "*   `|a - x| < |b - x|`, or\n",
    "*   `|a - x| == |b - x|` and `a < b`\n",
    "\n",
    "**Example 1:**\n",
    "\n",
    "```\n",
    "Input: arr = [1,2,3,4,5], k = 4, x = 3\n",
    "Output: [1,2,3,4]\n",
    "```\n",
    "\n",
    "**Example 2:**\n",
    "\n",
    "```\n",
    "Input: arr = [1,2,3,4,5], k = 4, x = -1\n",
    "Output: [1,2,3,4]\n",
    "```\n",
    "\n",
    "**Constraints:**\n",
    "\n",
    "*   `1 <= k <= arr.length`\n",
    "*   `1 <= arr.length <= 10<sup>4</sup>`\n",
    "*   Absolute value of elements in the array and `x` will not exceed `10<sup>4</sup>`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "abs(-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 a b\n",
      "1 c d\n"
     ]
    }
   ],
   "source": [
    "for i,item in enumerate({'a': 'b', 'c':'d'}.items()):\n",
    "    k,v = item\n",
    "    print(i,k,v)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "https://leetcode.com/problems/find-k-closest-elements\n",
    "\n",
    "\n",
    "\n",
    "Runtime: 412 ms, faster than 7.79% of Python3 online submissions for Find K Closest Elements.\n",
    "Memory Usage: 17.6 MB, less than 5.39% of Python3 online submissions for Find K Closest Elements.\n",
    "\n",
    "\n",
    "\n",
    "```python\n",
    "class Solution:\n",
    "    def findClosestElements(self, arr: List[int], k: int, x: int) -> List[int]:\n",
    "        length = k\n",
    "        index_dict = {i:abs(v-x) for i, v in enumerate(arr)}\n",
    "        sorted_dict = {k: v for k, v in sorted(index_dict.items(), key=lambda item: (item[1], item[0]))}\n",
    "        target_index = []\n",
    "        for i,item in enumerate(sorted_dict.items()):\n",
    "            k,v = item\n",
    "            if len(target_index) < length:\n",
    "                target_index.append(k)\n",
    "        values = []\n",
    "        for k in target_index:\n",
    "            values.append(arr[k])\n",
    "        values.sort()\n",
    "        return values\n",
    "```\n",
    "\n",
    "\n",
    "Spent 0.5 hour\n",
    "1 attempt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
